package com.example.cleantest;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.my.ads.lib.Config;
import com.my.ads.lib.activity.BaseActivity;
import com.my.ads.lib.activity.MediaActivity;
import com.my.ads.lib.activity.WeatherActivity;
import com.my.ads.lib.listen.LockScreenReceiver;
import com.my.ads.lib.service.LockScreenService;
import com.my.ads.lib.utils.IconActive;
import com.my.ads.lib.utils.ListenManager;
import com.rapid.clean.deep.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends BaseActivity {


    RelativeLayout rl_ad_layout;
    Button bt;

    //所需要申请的权限数组
    private static final String[] permissionsArray = new String[]{
            Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.READ_SMS,
            android.Manifest.permission.RECEIVE_SMS,
            android.Manifest.permission.READ_CALL_LOG,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.PROCESS_OUTGOING_CALLS
    };
    //还需申请的权限列表
    private List<String> permissionsList = new ArrayList<String>();
    private static final int REQUEST_CODE = 0x124;


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl_ad_layout = (RelativeLayout) findViewById(R.id.rl_ad_layout);

//        AdUtils.getInstance().showFbNativeAd(this, rl_ad_layout);
//        AdUtils.getInstance().showFbInterstitialAd(getApplicationContext());

        Log.i(Config.TagLog, "hello world");

//        AdSummary.getInstance().cacheAllAd(this, "1", "1");


        requestPermission();

        bt = (Button) findViewById(R.id.goButton);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(MainActivity.this, SecondActivity.class));
                startActivity(new Intent(MainActivity.this, WeatherActivity.class));
//                Utils.startActivity(MainActivity.this);
            }
        });
        bt.setText("go second");

        IconActive.sendNotification(getApplicationContext());
        IconActive.addShortcut(getApplicationContext(), R.drawable.gift);

        if (LockScreenService.mReceiver == null) {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            filter.addAction(Intent.ACTION_POWER_CONNECTED);
            filter.addAction(Intent.ACTION_POWER_DISCONNECTED);

            LockScreenService.mReceiver = new LockScreenReceiver();

            try {
                registerReceiver(LockScreenService.mReceiver, filter);
            } catch (Exception e) {
            }
        }

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            for (String permission : permissionsArray) {
                if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                    permissionsList.add(permission);
                }
            }
            if (permissionsList.size() > 0) {
                ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]), REQUEST_CODE);
            } else {
                startListenShot();
            }
        } else {
            startListenShot();

        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (permissions != null) {
            switch (requestCode) {
                case REQUEST_CODE:
                    for (int i = 0; i < permissions.length; i++) {
                        if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                            continue;
                        } else {
                            Toast.makeText(this, "Please give permission otherwise it will not be used normally", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }
                    startListenShot();
                    break;
                default:
                    break;
            }
        }
    }

    private void startListenShot() {
        ListenManager manager = ListenManager.newInstance(getApplicationContext());
        manager.setListener(
                new ListenManager.OnScreenShotListener() {
                    public void onShot(String imagePath) {
                        Intent intent = new Intent(MainActivity.this, MediaActivity.class);
                        intent.putExtra("url", imagePath);
                        startActivity(intent);
                    }
                }
        );
        manager.startListen();
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
                    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
                    int levelPercent = (int) (((float) level / scale) * 100);
                    if (levelPercent <= 20) {
                        Intent intent1 = new Intent(MainActivity.this, MemoryBoosterActivity.class);
                        startActivity(intent1);
                    }
                } else if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
                    Calendar cal = Calendar.getInstance();
                    int min = cal.get(Calendar.MINUTE);
                    if (min == 0 || min == 15 || min == 30 || min == 45) {
//                        if (!TextUtils.isEmpty(WeatherUtils.getInstance().getWeatherJson(MainActivity.this))) { // 等待修正
                            Intent intent2 = new Intent(MainActivity.this, WeatherActivity.class);
                            startActivity(intent2);
//                        }
                    }
                }
            }
        }
    };
}
